package classesJpa;

import classesBancodeDados.Permissao;
import classesJpa.exceptions.NonexistentEntityException;
import classesJpa.exceptions.PreexistingEntityException;
import classesMiolo.MapasPermissao;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.transaction.UserTransaction;

public class PermissaoJpaController implements Serializable {

    public PermissaoJpaController() {
        emf = Persistence.createEntityManagerFactory("Juliano_MoreiraPU");
    }
    
    public PermissaoJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public Permissao findPermissao(int id){
        Integer i = Integer.valueOf(id);
        EntityManager em = getEntityManager();
        try{
            Query q = em.createQuery("select object(o) from Permissao as o where id = '" + i + "'");
            return (Permissao)q.getSingleResult();
        } catch(NoResultException ex) {
            return null;
        }finally{
            em.close();
        }
    }
    
    public Permissao findPermissao(String id){
        MapasPermissao mapa = MapasPermissao.getInstance();
        Integer i = mapa.getFicha().get(id);
        EntityManager em = getEntityManager();
        try{
            Query q = em.createQuery("select object(o) from Permissao as o where id = '" + i + "'");
            return (Permissao)q.getSingleResult();
        } catch(NoResultException ex) {
            return null;
        }finally{
            em.close();
        }
    }
    
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Permissao permissao) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(permissao);
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findPermissao(permissao.getId()) != null) {
                throw new PreexistingEntityException("Permissao " + permissao + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Permissao permissao) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            permissao = em.merge(permissao);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = permissao.getId();
                if (findPermissao(id) == null) {
                    throw new NonexistentEntityException("The permissao with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Permissao permissao;
            try {
                permissao = em.getReference(Permissao.class, id);
                permissao.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The permissao with id " + id + " no longer exists.", enfe);
            }
            em.remove(permissao);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Permissao> findPermissaoEntities() {
        return findPermissaoEntities(true, -1, -1);
    }

    public List<Permissao> findPermissaoEntities(int maxResults, int firstResult) {
        return findPermissaoEntities(false, maxResults, firstResult);
    }

    private List<Permissao> findPermissaoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Permissao as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Permissao findPermissao(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Permissao.class, id);
        } finally {
            em.close();
        }
    }

    public int getPermissaoCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Permissao as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
